package cn.edu.scau.dao;

import cn.edu.scau.entity.RecoveryRegister;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@CacheNamespace
@Repository
public interface RecoveryRegisterMapper {
    /**
     * 根据编号删除康复登记
     * @param recoveryregisterid
     * @return
     */
    @Delete({
            "delete from recoveryregister",
            "where recoveryRegisterID = #{recoveryregisterid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer recoveryregisterid);

    /**
     * 根据登记编号、护士编号、客户编号、康复计划新增登记信息,不考虑null
     * @param record
     * @return
     */
    @Insert({
            "insert into recoveryregister (recoveryRegisterID, nurseID, ",
            "customerID, recoveryRegisterPlan)",
            "values (#{recoveryregisterid,jdbcType=INTEGER}, #{nurseid,jdbcType=INTEGER}, ",
            "#{customerid,jdbcType=INTEGER}, #{recoveryregisterplan,jdbcType=VARCHAR})"
    })
    int insert(RecoveryRegister record);

    /**
     * 根据登记编号、护士编号、客户编号、康复计划新增登记信息,考虑null。若为空，不向数据库插入数据
     * @param record
     * @return
     */
    @Options(useGeneratedKeys = true,keyColumn = "recoveryRegisterID",keyProperty = "recoveryRegisterID")
    @InsertProvider(type=RecoveryRegisterSqlProvider.class, method="insertSelective")
    int insertSelective(RecoveryRegister record);

    /**
     * 根据康复编号查找信息，不会查找护士名和客户名
     * @param recoveryRegisterID
     * @return
     */
    @Select({
            "select",
            "recoveryRegisterID, nurseID, customerID, recoveryRegisterPlan",
            "from recoveryregister",
            "where recoveryRegisterID = #{recoveryRegisterID,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column="recoveryRegisterID", property="recoveryRegisterID", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="nurseID", property="nurseID", jdbcType=JdbcType.INTEGER),
            @Result(column="customerID", property="customerID", jdbcType=JdbcType.INTEGER),
            @Result(column="recoveryRegisterPlan", property="recoveryRegisterPlan", jdbcType=JdbcType.VARCHAR)
    })
    RecoveryRegister selectByPrimaryKeyNoName(Integer recoveryRegisterID);
    
    /**
     * 根据康复编号查找信息，会查找护士名和客户名
     * @param recoveryRegisterID
     * @return
     */
    @Select({
        "select",
        "recoveryRegisterID, nurseID, nurseName, customerID, customerName, recoveryRegisterPlan",
        "from recoveryregister natural join customer natural join nurse",
        "where recoveryRegisterID = #{recoveryRegisterID,jdbcType=INTEGER}"
        })
    @Results({
        @Result(column="recoveryRegisterID", property="recoveryRegisterID", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="nurseID", property="nurseID", jdbcType=JdbcType.INTEGER),
        @Result(column="nurseName", property="nurseName", jdbcType=JdbcType.VARCHAR),
        @Result(column="customerID", property="customerID", jdbcType=JdbcType.INTEGER),
        @Result(column="customerName", property="customerName", jdbcType=JdbcType.VARCHAR),
        @Result(column="recoveryRegisterPlan", property="recoveryRegisterPlan", jdbcType=JdbcType.VARCHAR)
        })
    RecoveryRegister selectByPrimaryKey(Integer recoveryRegisterID);
    

    /**
     * 根据护士编号查找康复登记信息
     * @param nurseID
     * @return
     */
    @Select({
            "select",
            "recoveryRegisterID, nurseID, customerID, recoveryRegisterPlan",
            "from recoveryregister",
            "where nurseID = #{nurseID,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column="recoveryRegisterID", property="recoveryRegisterID", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="nurseID", property="nurseID", jdbcType=JdbcType.INTEGER),
            @Result(column="customerID", property="customerID", jdbcType=JdbcType.INTEGER),
            @Result(column="recoveryRegisterPlan", property="recoveryRegisterPlan", jdbcType=JdbcType.VARCHAR)
    })
    List<RecoveryRegister> selectByNurseIDNoName(Integer nurseID);
    
    /**
     * 根据护士编号查找康复登记信息，会查找名字
     * @param nurseID
     * @return
     */
    @Select({
        "select",
        "recoveryRegisterID, nurseID, nurseName, customerID, customerName, recoveryRegisterPlan",
        "from recoveryregister natural join customer natural join nurse",
        "where nurseID = #{nurseID,jdbcType=INTEGER}"
        })
    @Results({
    	@Result(column="recoveryRegisterID", property="recoveryRegisterID", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="nurseID", property="nurseID", jdbcType=JdbcType.INTEGER),
        @Result(column="nurseName", property="nurseName", jdbcType=JdbcType.VARCHAR),
        @Result(column="customerID", property="customerID", jdbcType=JdbcType.INTEGER),
        @Result(column="customerName", property="customerName", jdbcType=JdbcType.VARCHAR),
        @Result(column="recoveryRegisterPlan", property="recoveryRegisterPlan", jdbcType=JdbcType.VARCHAR)
        })
    List<RecoveryRegister> selectByNurseID(Integer nurseID);

    /**
     * 查找所有康复登记信息
     * @return
     */
    @Select({
            "select",
            "recoveryRegisterID, nurseID, customerID, recoveryRegisterPlan",
            "from recoveryregister"
    })
    List<RecoveryRegister> selectAllNoName();
    
    @Select({
        "select",
        "recoveryRegisterID, nurseID, nurseName, customerID, customerName, recoveryRegisterPlan",
        "from recoveryregister natural join customer natural join nurse"
        })
    List<RecoveryRegister> selectAll();

    /**
     *根据登记编号、护士编号、客户编号、康复计划修改登记信息,考虑null。若为空，不会更改数据库数据
     * @param record
     * @return
     */
    @UpdateProvider(type=RecoveryRegisterSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(RecoveryRegister record);

    /**
     * 根据登记编号、护士编号、客户编号、康复计划修改登记信息,不考虑null。若为空，会将对应属性修改为空
     * @param record
     * @return
     */
    @Update({
            "update recoveryregister",
            "set nurseID = #{nurseid,jdbcType=INTEGER},",
            "customerID = #{customerid,jdbcType=INTEGER},",
            "recoveryRegisterPlan = #{recoveryregisterplan,jdbcType=VARCHAR}",
            "where recoveryRegisterID = #{recoveryregisterid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(RecoveryRegister record);
    
    /**
     * 根据康复登记编号、护士编号、护士名、客户编号、客户名组合模糊查询
     * @param recoveryRegisterID
     * @param nurseID
     * @param nurseName
     * @param customerID
     * @param customerName
     * @return List<RecoveryRegister>
     */
    @SelectProvider(type = RecoveryRegisterSqlProvider.class,method = "find")
    List<RecoveryRegister> find(@Param("recoveryRegisterID") Integer recoveryRegisterID,@Param("nurseID") Integer nurseID,@Param("nurseName") String nurseName,@Param("customerID") Integer customerID,@Param("customerName") String customerName);
    
    /**
     * 根据康复登记编号、客户编号、客户名组合模糊查询，用户编号已确定
     * @param recoveryRegisterID
     * @param nurseID
     * @param nurseName
     * @param customerID
     * @param customerName
     * @return
     */
    @SelectProvider(type = RecoveryRegisterSqlProvider.class,method = "findWithNurseID")
    List<RecoveryRegister> findWithNurseID(@Param("recoveryRegisterID") Integer recoveryRegisterID,@Param("nurseID") Integer nurseID,@Param("customerID") Integer customerID,@Param("customerName") String customerName);
    
}